home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 235 / Issue 235 - September 2007 - DPCS0907DVD.ISO / Extras / NetObjects Fusion / NOF10.exe / data1.cab / FSI / lib / nof / net / Http.js < prev    next >
Encoding:
Text File  |  2007-04-11  |  7.3 KB  |  208 lines

  1. /****i* SOURCE_FILE/INFO
  2.   *
  3.   * NAME
  4.   *  Http.js
  5.   *
  6.   * USAGE
  7.   *  Part of Netobjects JavaScript Library.
  8.   *
  9.   * COPYRIGHT
  10.   *  Copyright ⌐ 2000-2005 Website Pros, Inc.
  11.   *  All Rights Reserved.
  12.   *
  13.   *  This is an unpublished work protected by Website Pros, Inc.
  14.   *  as a trade secret, and is not to be used or disclosed except as
  15.   *  expressly provided in a written license agreement executed by
  16.   *  you and Website Pros, Inc.
  17.   *
  18.   *      <copyright@websitepros.com>
  19.   *
  20.   * NOTES
  21.   *  JavaScript code.
  22.   *
  23.   *****/
  24. if (!IS_isModuleInitialized("IS.NOF.NET.Http"))
  25. {
  26.   
  27.   /****h* NOF_JavaScript_Library/NOF.NET.Http
  28.     *
  29.     * NAME
  30.     *  NOF.NET.Http
  31.     *
  32.     * DESCRIPTION
  33.     *    
  34.     *
  35.     ****/
  36.   
  37.   /**
  38.     * Constructor
  39.     * @param request - the request object;
  40.     */
  41.   function NET_Http( /*NET.HttpRequest*/ request ) {
  42.     this.__proto__ = NET_Http.prototype;        
  43.       
  44.     this.request    = request; 
  45.   }
  46.   {
  47.     var member = NET_Http.prototype;    
  48.     member.CLASS_NAME            = "NET.Http";
  49.     
  50.     var method = NET_Http.prototype;                                        
  51.     /**
  52.     * Checks the online mode for the specified URL. 
  53.     * @param request (optional if the constructor was invoked with a HttpRequest object)
  54.     * @return one of the following values:
  55.     *    -1: The argument does not specify an external address.            
  56.     *    0: The URL is online.            
  57.     *    1: The URL could not be connected.
  58.     **/        
  59.     method.isOnline = function (/*NET.HttpRequest*/ request) { 
  60.         var METHOD_NAME = "isOnline";
  61.         var _req = (request != null) ? request : this.request;                
  62.         if (_req != null) {                                
  63.             var pUrl = (typeof(_req) == 'string') ? _req : _req.URL;
  64.             return NOF.App.getFSIApp().OnlineMode(pUrl, false);
  65.         }        
  66.         return -1;
  67.     }        
  68.             
  69.             
  70.     /**
  71.     * Performs a request on a given HttpRequest object.
  72.     * @param request (optional if the constructor was invoked with a HttpRequest object)
  73.     * @return a NOF.NET.HttpResponse object
  74.     **/        
  75.     method.open = function (/*NOF.NET.HttpRequest*/ request) { 
  76.       var METHOD_NAME = "open";
  77.       var _req = (request != null) ? request : this.request;
  78.       var resp = null;
  79.       
  80.       if (_req != null) {            
  81.         // read request URL                                         
  82.         var pUrl = _req.URL;                
  83.         //TODO:    validate this URL properly!
  84.         if ( (pUrl == null) || (pUrl.indexOf("http") != 0) ) return null;                
  85.         var    pHeader = "", 
  86.           pPostData = ""; 
  87.         // read request headers
  88.         var heads = _req.getHeaders();
  89.         pHeader = this.convertHTTPHeaders(heads); //StringToHashMap
  90.         
  91.         var pIsGet = (_req.getMethod() == "GET");
  92.         if (!pIsGet) {
  93.           // read data to be posted only if it's a POST request
  94.           pPostData = _req.getData();
  95.         }
  96.         
  97.         var pIncludeHeader    = _req.getIncludeHeaderResult(); 
  98.         var    pIncludeMessage = _req.getIncludeBodyResult(); 
  99.           
  100.         var respStr = NOF.App.getFSIApp2().HttpRequest2(pUrl, pHeader, pPostData, pIsGet, pIncludeHeader, pIncludeMessage);            
  101.         
  102.         resp = new NOF.NET.HttpResponse();
  103.         resp.URL = pUrl;
  104.         
  105.         if (respStr != null && respStr.length > 0) {
  106.           if (!pIncludeHeader) {
  107.             if (pIncludeMessage) {
  108.               resp.content = respStr;
  109.             }                    
  110.           } else { // include headers
  111.             if (!pIncludeMessage) { // only headers
  112.               resp.headers = this.convertHTTPHeaders(respStr);                                                    
  113.             } else {                            
  114.               var parsedResponse = this.parseStream(respStr);                            
  115.               resp.headers = this.convertHTTPHeaders(parsedResponse.headers);
  116.               resp.content = parsedResponse.content;                            
  117.             }
  118.             resp.statusCode = resp.getHeader("status-code");
  119.           }
  120.         }                
  121.       }
  122.       return resp;
  123.     }
  124.     
  125.     /**
  126.     * Download the content of a request to a specified (local) location.
  127.     * @param localPath - the path (including the name) where the response file will be saved.
  128.     * @param pUseProgress true if you want to show a progress bar during download.
  129.     * @param request (optional if the constructor was invoked with a HttpRequest object)
  130.     * 
  131.     * @return true if the download was succesfull. 
  132.     **/        
  133.     method.download = function ( /*String*/ localPath, /*boolean*/ pUseProgress, /*NOF.NET.HttpRequest*/ request) { 
  134.       var METHOD_NAME = "download";
  135.       var _req = (request != null) ? request : this.request;            
  136.  
  137.       //TODO:        validate URL            
  138.       if ( (_req != null) && (_req.URL != null) && (_req.URL.indexOf("http") == 0) ) {            
  139.         
  140.         var url = _req.URL;
  141.         var postData = _req.getData();
  142.         
  143.         var iQMark = url.indexOf("?");
  144.         if (iQMark > -1) {                    
  145.           postData = (postData.length > 0) ? (postData + "&" + url.substring(iQMark + 1, url.length)) : (url.substring(iQMark + 1, url.length));
  146.           url = url.substring(0, iQMark);
  147.           _req.setMethod("POST");
  148.         }       
  149.         
  150.         var downloadResult = NOF.App.getFSIApp().HttpDownload(url, this.convertHTTPHeaders(_req.getHeaders()), postData, (_req.getMethod() == "GET"), localPath, pUseProgress);
  151.         return downloadResult;                
  152.       }                
  153.       
  154.       return false;
  155.     }        
  156.     
  157.     //private method
  158.     method.convertHTTPHeaders = function ( headersObj ) {
  159.       var retObj = null;
  160.       if (headersObj != null) {
  161.         if (typeof(headersObj.join) == "function") { //it is an array (or Hash)
  162.           retObj = "";
  163.           for (var h in headersObj) {
  164.             //if (h == "status-code") continue; //exclude Status-Code?
  165.             if (typeof(headersObj[h]) == "object" || typeof(headersObj[h]) == "string") {
  166.               retObj += ("" + h + ": " + headersObj[h] + "\r\n");
  167.             }
  168.           }
  169.         } else if ( (typeof(headersObj) == "string") || (typeof(headersObj.substring) == "function") ) {
  170.           retObj = new Array();                
  171.           // get Status-Code
  172.           //HTTP/1.1 200 OK     or     HTTP/1.1 404 Object Not Found
  173.           if (headersObj.indexOf("HTTP/") == 0) {
  174.             //get index of the end of the first line                         
  175.             var iEOfL = headersObj.indexOf("\r\n"); 
  176.             
  177.             var iOfSpace = headersObj.indexOf(" ");
  178.             var statusCode = headersObj.substring(iOfSpace + 1, headersObj.indexOf(" ", iOfSpace + 1));
  179.             retObj["status-code"] = statusCode;
  180.             
  181.             headersObj = headersObj.substring(iEOfL + 2);
  182.           }
  183.           var hList = headersObj.split("\r\n");
  184.           var hItem = null;
  185.           var tmpList = null;
  186.           for (var i = 0; i < hList.length; i++) {
  187.             hItem = hList[i];
  188.             if (hItem.length == 0) continue;
  189.             tmpList = hItem.split(":");
  190.             // all the headers name contains only lowercase letters
  191.             retObj[tmpList[0].toLowerCase()] = tmpList[1].substring(1);
  192.           }                        
  193.         }
  194.       }
  195.       return retObj;
  196.     }
  197.     
  198.     method.parseStream = function (str) {
  199.       var obj = new Object();
  200.       iOfFirstEmptyLine = str.indexOf("\r\n\r\n");
  201.       obj.headers = str.substring(0, iOfFirstEmptyLine);
  202.       obj.content = str.substring(iOfFirstEmptyLine + "\r\n\r\n".length);
  203.       return obj;
  204.     }
  205.   }
  206.   
  207.   NOF.NET.__proto__.Http = NET_Http;
  208. }